//
//  MineAddressViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/29.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineAddressViewController.h"
#import "MineAddressTableViewCell.h"
#import "MineAddressAddTableViewCell.h"

#import "MineAddressAddViewController.h"
#import "MineAddressChangeViewController.h"

@interface MineAddressViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *addAddressButton;
@property (weak, nonatomic) IBOutlet UIView *haveView;
@property (weak, nonatomic) IBOutlet UIView *noView;
@property (weak, nonatomic) IBOutlet UITableView *addressTableView;

@property (retain,nonatomic) NSArray *addressList;

@end

@implementation MineAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self obationData];
    [self UI];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(change)
                                                 name:@"addrChange"
                                               object:nil];
}

- (void)UI {
    
    [((TabBarController *)self.tabBarController) setTabbarHidden:YES];
    self.addAddressButton.layer.cornerRadius = 20;
    self.title = @"我的地址";
    [self haveAddress];
}


- (void)change {
    
    [self obationData];
}

- (void)obationData {
    
    NSDictionary *dic = @{@"user_id":[userInfoTool share].ID};
    [WLHttpRequestAfter userGetUserAddressWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if (resultDictionary) {
            
            self.addressList = resultDictionary[@"data"];
        }
        [self.addressTableView reloadData];
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)haveAddress {
    
    self.addressTableView.delegate = self;
    self.addressTableView.dataSource = self;
    self.addressTableView.backgroundColor = Color_Back_Black;
    self.addressTableView.separatorColor = [UIColor clearColor];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if(section == 0) {
        
        return self.addressList.count;
    }else {
        
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        MineAddressTableViewCell *cell = (MineAddressTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }else {
        
        MineAddressAddTableViewCell *cell = (MineAddressAddTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        MineAddressTableViewCell *cell = [MineAddressTableViewCell initWithTableView:tableView];
        cell.infoDic = self.addressList[indexPath.row];
        return cell;
    }else {
        
        MineAddressAddTableViewCell *cell = [MineAddressAddTableViewCell initWithTableView:tableView];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        [self.navigationController
        pushViewController:
          [[MineAddressChangeViewController alloc] initWithInfoDic:self.addressList[indexPath.row]]
        animated:YES];
        
    }else {
        
        [self.navigationController
        pushViewController:[[MineAddressAddViewController alloc] init]
        animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"addrChange" object:nil];
}


@end
